import { Injectable } from '@angular/core';
import { CanActivate,Router } from '@angular/router';
import { UserPageProvider } from '../pages/user/user.page.provider';
import { Observable } from 'rxjs/Observable';

@Injectable()

export class UserVerifyProvider implements CanActivate {

    constructor(private userProvider: UserPageProvider,private router :Router) { }

    canActivate(route, state): Observable<boolean> {

        this.userProvider.verify().subscribe((res: any) => {
            if (res['success']) {

                return Observable.of(true);

            } else {
                localStorage.removeItem('token');
                this.router.navigate(['/login']);

                return Observable.of(false);
            }
        }, (err: any) => {
            localStorage.removeItem('token');
            this.router.navigate(['/login']);

            return Observable.of(false);

        });

        return Observable.of(true);

    }
}